package org.maxgroundstation.groundstation.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import org.maxgroundstation.groundstation.comm.service.gps.PointOfInterest;

@SuppressWarnings("serial")
public class PointOfInterestCreationDialog extends JDialog {
    
    // Static flag indicating if POI was created or canceled.
    private static boolean CREATE_POI = false;
    
    // Data members
    private PointOfInterest poi;
    private DecimalFormat positionFormatter;
    private DateFormat dateFormatter;
    private JTextField nameTF;
    private JTextArea descriptionTA;
    
    /**
     * Constructor
     * @param owner the top JFrame owner of dialog.
     * @param poi the point of interest.
     */
    private PointOfInterestCreationDialog(JFrame owner, PointOfInterest poi) {
        super(owner, "Create a Point of Interest", true);
        
        // Initialize data members.
        this.poi = poi;
        positionFormatter = new DecimalFormat("0.000000");
        dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        setLayout(new BorderLayout());
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                closeDialog(false);
            }
        });
        
        JPanel infoPanel = new JPanel(new GridLayout(3, 1));
        add(infoPanel, BorderLayout.NORTH);
        
        JPanel latitudePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        infoPanel.add(latitudePanel);
        
        JLabel latitudeLabel = new JLabel("Latitude: " + positionFormatter.format(poi.getLatitude()));
        latitudePanel.add(latitudeLabel);
        
        JPanel longitudePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        infoPanel.add(longitudePanel);
        
        JLabel longitudeLabel = new JLabel("Longitude: " + positionFormatter.format(poi.getLongitude()));
        longitudePanel.add(longitudeLabel);
        
        JPanel timestampPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        infoPanel.add(timestampPanel);
        
        JLabel timestampLabel = new JLabel("Time: " + dateFormatter.format(poi.getTimestamp()));
        timestampPanel.add(timestampLabel);
        
        JPanel dataPanel = new JPanel(new BorderLayout());
        add(dataPanel, BorderLayout.CENTER);
        
        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        dataPanel.add(namePanel, BorderLayout.NORTH);
        
        JLabel nameLabel = new JLabel("Name: ");
        namePanel.add(nameLabel);
        
        nameTF = new JTextField(25);
        namePanel.add(nameTF);
        
        JPanel descriptionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        descriptionPanel.setBorder(new TitledBorder("Description"));
        dataPanel.add(descriptionPanel, BorderLayout.CENTER);
        
        descriptionTA = new JTextArea("", 10, 30);
        descriptionPanel.add(descriptionTA);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(buttonPanel, BorderLayout.SOUTH);
        
        JButton createButton = new JButton("Create");
        createButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                populatePoi();
                closeDialog(true);
            }
        });
        buttonPanel.add(createButton);
        
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeDialog(false);
            }
        });
        buttonPanel.add(cancelButton);
        
        pack();
        
        int xPos = owner.getX() + (owner.getWidth() / 2) - (getWidth() / 2);
        int yPos = owner.getY() + (owner.getHeight() / 2) - (getHeight() / 2);
        setLocation(new Point(xPos, yPos));
    }
    
    /**
     * Populates the point of interest name and description.
     */
    private void populatePoi() {
        poi.setName(nameTF.getText());
        poi.setDescription(descriptionTA.getText());
    }
    
    /**
     * Close the dialog and set the create flag.
     * @param createPoi true if creating POI.
     */
    private void closeDialog(boolean createPoi) {
        CREATE_POI = createPoi;
        dispose();
    }
    
    /**
     * Opens the create point of interest dialog.
     * @param owner the top JFrame owner of the dialog.
     * @param poi the point of interest to populate.
     * @return true if creating POI, false if not.
     */
    public static boolean openDialog(JFrame owner, PointOfInterest poi) {
        // Show dialog.
        PointOfInterestCreationDialog dialog = new PointOfInterestCreationDialog(owner, poi);
        dialog.setVisible(true);
        
        // Return result of dialog.
        return PointOfInterestCreationDialog.CREATE_POI;
    }
}